
package Servlets;

import Beans.User;
import Email.GMailManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Raed Shomali
 */
public class SessionTest extends HttpServlet
{
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
    {
        response.setContentType( "text/html;charset=UTF-8" );

        PrintWriter out = response.getWriter();

        try
        {
            // Create a new Session if no session already existed by passing "true"
            HttpSession session = request.getSession( true );

            // Print Session Information ...
            Date creationTime = new Date( session.getCreationTime() );
            Date lastAccessedTime = new Date( session.getLastAccessedTime() );
            
            out.println( "<b>Is New?</b> " + session.isNew() + "<br />" ) ;
            out.println( "<b>Created:</b> " + creationTime + "<br />" );
            out.println( "<b>Last Accessed:</b> " + lastAccessedTime + "<br /><br />" );
            
            User user = (User) session.getAttribute( "user" ) ;
            
            if( user != null )
            {
                out.println( "<b>First Name:</b> " + user.getFirstName() + "<br />");
                out.println( "<b>Last Name:</b> " + user.getLastName() + "<br />");
                out.println( "<b>Email Address:</b> " + user.getEmailAddress() + "<br /><br /><br />" );
            }
        }
        finally
        {
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
    {
        processRequest( request , response );
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
    {
        processRequest( request , response );
    }

}
